package com.bblocks.common.anti_reptile.data;


import com.bblocks.common.anti_reptile.config.AntiReptileProperties;
import com.bblocks.common.anti_reptile.constant.AntiReptileConsts;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * <p>Project: bb-anti-reptile - DatabaseConfig</p>
 * <p>描述：</p>
 *
 * @Author Zhao [125043150@qq.com]
 * @Date 2024/4/1 11:23
 * @Version 1.0
 * @since 8
 */
@ConditionalOnProperty(name = "anti.reptile.db.url", matchIfMissing = false)
public class DatabaseConfig {
    @Bean("dataSourceAnti")
    public DataSource dataSource(AntiReptileProperties antiReptileProperties) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(antiReptileProperties.getDb().getUrl());
        dataSource.setUsername(antiReptileProperties.getDb().getUsername());
        dataSource.setPassword(antiReptileProperties.getDb().getPassword());
        dataSource.setDriverClassName(antiReptileProperties.getDb().getDriverClassName());
        return dataSource;
    }

    @Bean(AntiReptileConsts.JDBC_TEMP_ANTI)
    public JdbcTemplate jdbcTemplate(DataSource dataSourceAnti) {
        return new JdbcTemplate(dataSourceAnti);
    }


}
